<?php
/**
 * API日志记录
 * 
 * @author:dawei
 * @copyright 139.com Inc
 * 
 */

class ApiLog {

	static private $_log = '/home/d139/logs/openapi/open_platform_api.[split].log';//全量日志
	static private $_access_log = '/home/d139/logs/openapi/open_platform_api_access.[split].log';//轻量访问日志
	static private $_reqlimit_log = '/home/d139/logs/openapi/open_platform_api_reqlimit.[split].log';//访问限制日志

	static private $_ugc_api = array(	'timeline_posttext',
										'timeline_postpict',
										'timeline_postvideo',
										'timeline_comment',
										'timeline_reply',
										'timeline_repost',
										'timeline_destory',
										'account_login',
										'account_activate',
										'mobile_creat',
										'user_creat',
										'relationship_creat',
										'relationship_destory'
									);

	static private $_ugc_sapi = array(	'posttext.json',
										'postpict.json',
										'postvoide.json',
										'reply.json',
										'destory.json',
										'login.json',
										'activate.json',
										'create.json'
									);
	/**
	 * 开放平台API日志
	 * 
	 * @param unknown_type $request_api
	 * @param unknown_type $appid
	 * @param unknown_type $output
	 * @param unknown_type $error_code
	 * @param unknown_type $rest_api_ec 第三方接口错误代码
	 * @param unknown_type $execution_time 程序执行时间[毫秒]
	 */
	static public function Log($request_api, $appid, $output, $error_code, $rest_api_ec, $execution_time=null) {

		global $invoke_controller;
		global $invoke_method;

		$split_data = date('Ymd');
		$log_file = str_replace('[split]', $split_data, self::$_log);
		$access_log_file = str_replace('[split]', $split_data, self::$_access_log);
		ApiLog::ignore_msg_api($request_api,$_POST);

		$ip = ApiUtility::GetRemoteIp();
		$input = 'GET:' . json_encode($_GET);
		$input .= ', POST:' . json_encode($_POST);
		$output = json_encode($output);
		$date = date('Y-m-d H:i:s');
		//access log
		if (ACCESS_LOG == 'TRUE'){
			$ret_type = $error_code == '0' ? 'succ' : 'error';
			$access_msg = "$ip - $date $appid, $invoke_controller/$invoke_method, $ret_type:$error_code, $rest_api_ec, $execution_time\n";
			error_log($access_msg, 3, $access_log_file);
		}

		if (!in_array ($invoke_controller . "_" . $invoke_method, self::$_ugc_api)){
			if (true === API_LOG_ONLY_ERROR && $error_code == 0) {
					return ;
			}
		}

		if (API_LOG_MODE == 'SIMPLE'){
			$output = substr($output, 0, 200) . '...';
		}

		//all request log
		$msg = "$ip [$date] appid:$appid, $request_api, error_code:$error_code, sapi_code:$rest_api_ec, exe_time:$execution_time, $input, - - ,$output \n\n";
		error_log($msg, 3, $log_file);
	}
	/**
	 * 开放平台访问SAPI日志
	 * 
	 * @param json $result
	 * @param string $interface
	 * @param array $input
	 */
	static public function SapiLog($result, $interface, $input, $execution_time=null) {

		$path_parts = pathinfo($interface);

		if(!in_array($path_parts['basename'], self::$_ugc_sapi)){
			if (true === API_LOG_ONLY_ERROR && Api_SapiData::IsSucceed(json_decode($result, 1))) {
				return ;
			}
		}

		$split_data = date('Ymd');
		$log_file = str_replace('[split]', $split_data, self::$_log);

		ApiLog::ignore_msg_sapi($interface,$input);

		$ip = ApiUtility::GetRemoteIp();
		$input = json_encode($input);
		$date = date('Y-m-d H:i:s');

		if (API_LOG_MODE == 'SIMPLE') {
			$result = substr($result, 0, 200) . '...';
		}

		$msg =  "$ip [$date] $interface, exe_time:$execution_time, $input, - -, $result";

		error_log($msg . "\n", 3, $log_file);
	}

	/**
	 * 开放平台访问OpApi日志
	 * 
	 * @param json $result
	 * @param string $interface
	 * @param array $input
	 */
	static public function OpapiLog($result, $interface, $input, $execution_time=null) {
		$split_data = date('Ymd');
		$log_file = str_replace('[split]', $split_data, self::$_log);

		$ip = ApiUtility::GetRemoteIp();
		$input = json_encode($input);
		$date = date('Y-m-d H:i:s');

		if (API_LOG_MODE == 'SIMPLE') {
			$result = substr($result, 0, 200) . '...';
		}

		$msg =  "$ip [$date] $interface, exe_time:$execution_time, $input, - -, $result";

		error_log($msg . "\n", 3, $log_file);
	}

	/**
	 * 开放平台访问广东开放平台日志
	 * 
	 * @param json $result
	 * @param string $interface
	 */
	static public function OapiLog($result, $interface, $execution_time=null) {
		$split_data = date('Ymd');
		$log_file = str_replace('[split]', $split_data, self::$_log);

		$ip = ApiUtility::GetRemoteIp();
		$input = json_encode($input);
		$date = date('Y-m-d H:i:s');

		if (API_LOG_MODE == 'SIMPLE') {
			$result = substr($result, 0, 200) . '...';
		}

		$msg =  "$ip [$date] $interface, exe_time:$execution_time, - -, $result";

		error_log($msg . "\n", 3, $log_file);
	}

	//网盘接口日志
	static public function NetDiskApiLog($result, $interface, $input, $execution_time=null) {
		$split_data = date('Ymd');
		$log_file = str_replace('[split]', $split_data, self::$_log);

		$ip = ApiUtility::GetRemoteIp();
		//$input = json_encode($input);
		$date = date('Y-m-d H:i:s');

		if (API_LOG_MODE == 'SIMPLE') {
			$result = substr($result, 0, 200) . '...';
		}

		$msg =  "$ip [$date] $interface, exe_time:$execution_time, $input, - -, $result";

		error_log($msg . "\n", 3, $log_file);
	}

	/**
	 * 开放平台访问限制日志，记录达到接口限制的app
	 *
	 * @param int $app_id
	 * @param int $app_level
	 * @param string $limit_type
	 * @param int $count 当前值
	 * @param int $limit_count 限制值
	 */
	static public function ReqlimitLog($app_id, $app_level, $limit_type, $count, $limit_count) {
		$split_data = date('Ymd');
		$log_file = str_replace('[split]', $split_data, self::$_reqlimit_log);

		$ip = ApiUtility::GetRemoteIp();
		$date = date('Y-m-d H:i:s');

		$msg =  "$ip [$date] $app_id, $app_level, $limit_type, $count, $limit_count";

		error_log($msg . "\n", 3, $log_file);
	}

	static public function ignore_msg_api($url,&$ignore){
		$ignore_msg_api = array('postpict','uploadpict');
		
		$apiname = pathinfo($url);
		if(in_array($apiname['filename'], $ignore_msg_api)){
			$size = strlen($ignore['pic'])/1.33;
			$ignore['size'] = Utility::byte_format($size);
			unset($ignore['pic']);
		}
	}
	
	static public function ignore_msg_sapi($url,&$ignore){
		$apiname = pathinfo($url);
		if($apiname['filename'] == 'postpict'){
			$size = strlen($ignore['resource'])/1.33;
			$ignore['size'] = Utility::byte_format($size);
			unset($ignore['resource']);
		}else if($apiname['filename'] == 'uploadPict'){
			$size = strlen($ignore['content'])/1.33;
			$ignore['size'] = Utility::byte_format($size);
			unset($ignore['content']);
		}
	}
}
